package com.turnengine.client.global.announcement.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.enums.GameLevel;
import java.io.IOException;

/**
 * The Post Announcement Data Serializer.
 */
public class PostAnnouncementDataSerializer extends AbstractCommandDataSerializer<Integer> implements IPostAnnouncementDataSerializer {

	/**
	 * Creates a new Post Announcement Data Serializer.
	 */
	public PostAnnouncementDataSerializer() {
		super(PostAnnouncement.COMMAND_ID);
	}

	@Override
	public Integer readReturnValue(IDataReader reader) throws IOException {
		Integer value = reader.readIntegerObject(true);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, Integer value) throws IOException {
		writer.writeIntegerObject(value, true);
	}

	@Override
	public IExecutableCommand<Integer> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int gameLevelId = reader.readInt();
		GameLevel gameLevel = reader.readEnum(GameLevel.class, true);
		String subject = reader.readString(true);
		String body = reader.readString(true);

		// Create object
		PostAnnouncement object = new PostAnnouncement();
		object.setLoginId(loginId);
		object.setGameLevelId(gameLevelId);
		object.setGameLevel(gameLevel);
		object.setSubject(subject);
		object.setBody(body);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<Integer> argument) throws IOException {
		PostAnnouncement object = (PostAnnouncement) argument;

		// Get fields
		long loginId = object.getLoginId();
		int gameLevelId = object.getGameLevelId();
		GameLevel gameLevel = object.getGameLevel();
		String subject = object.getSubject();
		String body = object.getBody();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(gameLevelId);
		writer.writeEnum(gameLevel, true);
		writer.writeString(subject, true);
		writer.writeString(body, true);
	}

	@Override
	public IExecutableCommandResponse<Integer> newResponse() {
		return new PostAnnouncementResponse();
	}
}
